package icu.xuyijie.springbootlearning2.chapter4.mail;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailSendException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.UnsupportedEncodingException;

/**
 * @author 徐一杰
 * @date 2025/03/10 10:47
 * @description 邮件发送工具类
 */
@Component
public class MailSenderUtils {

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    /**
     * 发送简单邮件
     * @param to 邮件接收人邮箱地址
     * @param subject 主题
     * @param content 内容
     */
    public void sendSimpleMail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        // 发件人
        message.setFrom(from);
        // 收件人
        message.setTo(to);
        // 邮件主题
        message.setSubject(subject);
        // 邮件正文
        message.setText(content);
        // 发送
        mailSender.send(message);
    }

    /**
     * 发送带样式的邮件
     * @param to 接收人
     * @param subject 主题
     * @param content 内容（可以写HTML标签<button style="color: pink">Register now</button>）
     */
    public void sendHtmlMail(String to, String subject, String content) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);

        try {
            mimeMessageHelper.setFrom(new InternetAddress(from, "昵称", "UTF8"));
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setText(content);
            mailSender.send(mimeMessage);
        } catch (Exception e) {
            System.out.println("邮件发送失败");
        }
    }

    /**
     * 发送带附件的邮件
     * @param to 接收人
     * @param subject 主题
     * @param content 内容
     * @param filePath 要附带的文件地址
     */
    public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(new InternetAddress(from, "昵称", "UTF8"));
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);

            //设置附件
            FileSystemResource file = new FileSystemResource(new File(filePath));
            String fileName = file.getFilename();
            helper.addAttachment(fileName, file);

            mailSender.send(message);
        } catch (MailSendException | MessagingException | UnsupportedEncodingException e) {
            System.out.println("发送附件邮件时发生异常");
        }
    }

}
